package com.android.qjpmc.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

import com.android.qjpmc.application_module.activity.StationActivity;
import com.android.qjpmc.data_module.PwrDataModule;
import com.android.qjpmc.kernel.PwrDiagramObject;
import com.android.qjpmc.kernel.StateCompt;

public class JxtView extends View
{
    StationActivity m_RtDataActive = null;

    protected int RunSign=0;
    private int TIME = 1000;

    public PwrDiagramObject m_JxtCnfg = null;
    public int m_Width=2160;
    public int m_JxtPos=0;

    public byte m_State[][];
    public float m_Value[][];

    public int m_OprTime=0;

    public PointF m_CenterPnt=new PointF();
    public float m_FShowRate=1;

    public JxtView(Context context, AttributeSet attrs) {
        super(context, attrs);
        handler.postDelayed(runnable, TIME); //每隔1s执行
        DisplayMetrics dm = getResources().getDisplayMetrics();
        m_CenterPnt.x=dm.widthPixels/2;
        m_CenterPnt.y=dm.heightPixels/2;


        final float scale = getResources().getDisplayMetrics().density;
        int differ=(int) (120 * scale + 0.5f);
        m_JxtCnfg = PwrDataModule.getInstance().GetPwrDiagramObject();

        //setTag("binding_jxt");
/*
            Active.screenWidth=dm.widthPixels;
            Active.screenHeight=dm.heightPixels;
            Active.screenHeight=dm.heightPixels-differ;
*/

//            LinearLayout lay = (LinearLayout) findViewById(R.id.ll_jxt);

//            lay.addView(imageView);
        //PwrDataModule.getInstance().
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // handler自带方法实现定时器
            try {
                handler.postDelayed(this, TIME);

                //全部刷新 2022年7月20日 调试
                invalidate();

/*
                if(m_JxtCnfg==null) return;
                PwrDiagramObject.Page JxtPage = m_JxtCnfg.m_Page.get(m_JxtPos);
                for(int i = 0; i<JxtPage.m_StateComptNum && i<JxtPage.m_StateComptList.size(); i++) {
                    StateCompt StateComptTemp=JxtPage.m_StateComptList.get(i);
                    if(StateComptTemp.m_ShowType==2) {
                        if(StateComptTemp.m_ShowPos>=StateComptTemp.m_StateNum) continue;
                        PwrDiagramObject.Element ElementTemp=StateComptTemp.m_Element.get(StateComptTemp.m_ShowPos);
                        RefreshRect(ElementTemp.m_StartPoint,ElementTemp.m_EndPoint);
                    }
                    else if(StateComptTemp.m_ShowType==1) {
                        PwrDiagramObject.Element ElementTemp=StateComptTemp.m_Element.get(0);
                        RefreshRect(ElementTemp.m_StartPoint,ElementTemp.m_EndPoint);
                    }
                }

                for(int i=0;i<JxtPage.m_ValueComptNum && i<JxtPage.m_ValueCompt.size();i++) {
                    PwrDiagramObject.ValueCompt ValueComptTemp=JxtPage.m_ValueCompt.get(i);
                    RefreshRect(ValueComptTemp.m_StartPoint, ValueComptTemp.m_EndPoint);
                }

 */

            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("exception...");
            }
        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //继承了Activity的onTouchEvent方法，直接监听点击事件
        if(RunSign==1)  return true;
        if(m_JxtCnfg==null)  return super.onTouchEvent(event);
        if(m_OprTime!=0) return super.onTouchEvent(event);
        int PressSign=0;
        RunSign=1;
        PointF point=new PointF(event.getX(), event.getY());
        PwrDiagramObject.Page JxtPage = m_JxtCnfg.m_Page.get(m_JxtPos);
        for(int i=0;i<JxtPage.m_NormalBtnNum && i<JxtPage.m_NormalBtn.size();i++) {
            PwrDiagramObject.NormalBtn BtnTemp = JxtPage.m_NormalBtn.get(i);

            int WidthEx=m_JxtCnfg.m_ScreenWidth;

            PointF StartPnt=GetPoint(BtnTemp.m_StartPoint);
            StartPnt.x=StartPnt.x * m_Width  / WidthEx;
            StartPnt.y=StartPnt.y * m_Width  / WidthEx;

            PointF EndPnt=GetPoint(BtnTemp.m_EndPoint);
            EndPnt.x=EndPnt.x * m_Width  / WidthEx;
            EndPnt.y=EndPnt.y * m_Width  / WidthEx;

            RectF rect = new RectF(StartPnt.x, StartPnt.y, EndPnt.x, EndPnt.y);

            if(rect.contains(point.x,point.y)) {
                if(BtnTemp.m_FuncType==1) {

/*


                        //遥控  2022年1月5日 15:54:41 屏蔽
                        m_strOprName=BtnTemp.m_BtnConText;
                        m_StationIndex = BtnTemp.m_DataIdx;

                        final EditText et = new EditText(Active);
                        et.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        new AlertDialog.Builder(Active).setTitle("下发"+m_strOprName+"命令，请输入密码:").setIcon(
                                android.R.drawable.ic_dialog_info).setView(et).setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                m_strInputPWD = et.getText().toString();
                                try {
                                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                    field.setAccessible(true);
                                    if(m_strOprPWD.equals(m_strInputPWD) ) {
                                        m_OprType = 1;
                                        m_OprTime=1;
                                        field.set(dialog, true);
                                        RunSign = 0;
                                    }
                                    else {
                                        CharSequence html1 = Html.fromHtml("<font color='blue'>密码不正确</font>");
                                        et.setError(html1);
                                        field.set(dialog, false);
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }) .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                try {
                                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                    field.setAccessible(true);
                                    field.set(dialog, true);
                                    RunSign = 0;
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }).show();
*/




                }
                else if(BtnTemp.m_FuncType==2){
                    //遥调
/*

2022年1月5日 15:55:45






                        m_strOprName=BtnTemp.m_BtnConText;
                        m_StationIndex = BtnTemp.m_DataIdx;

                        LayoutInflater factory = LayoutInflater.from(Active);
                        final View textEntryView = factory.inflate(R.layout.ra, null);
                        final EditText editpwd = (EditText) textEntryView.findViewById(R.id.editpwd);
                        final EditText editvalue = (EditText)textEntryView.findViewById(R.id.editvalue);

                        new AlertDialog.Builder(Active).setTitle("下发"+m_strOprName+"命令").setIcon(
                                android.R.drawable.ic_dialog_info).setView(textEntryView).setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                m_strInputPWD = editpwd.getText().toString();
                                try {
                                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                    field.setAccessible(true);
                                    if (m_strOprPWD.equals(m_strInputPWD)) {
                                        m_OprType = 2;
                                        m_OprTime = 1;
                                        field.set(dialog, true);
                                        String Value = editvalue.getText().toString();
                                        m_fSetValue = Float.valueOf(Value);

                                        RunSign = 0;

                                    } else {
                                        CharSequence html1 = Html.fromHtml("<font color='blue'>密码不正确</font>");
                                        editpwd.setError(html1);
                                        field.set(dialog, false);
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }) .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                try {
                                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                    field.setAccessible(true);
                                    field.set(dialog, true);
                                    RunSign = 0;
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }).show();
                    */



                }
                PressSign=1;
            }
        }
        if(PressSign==0)  RunSign=0;

        return super.onTouchEvent(event);
    }



    public void RefreshRect(String StartPnt,String EndPnt)
    {
        int WidthEx=m_JxtCnfg.m_ScreenWidth;

        PointF StartPoint=GetPoint(StartPnt);
        StartPoint.x=StartPoint.x * m_Width  / WidthEx;
        StartPoint.y=StartPoint.y * m_Width  / WidthEx;

        PointF EndPoint=GetPoint(EndPnt);
        EndPoint.x=EndPoint.x * m_Width  / WidthEx;
        EndPoint.y=EndPoint.y * m_Width  / WidthEx;
        Rect rect=new Rect((int)StartPoint.x,(int)StartPoint.y,(int)EndPoint.x,(int)EndPoint.y);
       // invalidate(rect); //重新绘制区域
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {

        PwrDataModule.getInstance().SetJxtPageIndex(m_JxtPos);
        PwrDataModule.getInstance().onDraw(canvas);
        /*
        if(m_JxtCnfg==null) return;
        PwrDiagramObject.Page JxtPage = m_JxtCnfg.m_Page.get(m_JxtPos);
        int BackClr=GetColor(JxtPage.m_BackColor);
        canvas.drawColor(BackClr);

        for(int i=0; i < JxtPage.m_RectNum && i< JxtPage.m_Rect.size(); i++)
        {
            PwrDiagramObject.Rect RectTemp=JxtPage.m_Rect.get(i);
            DrawRect(RectTemp, canvas);
        }
        for(int i = 0; i < JxtPage.m_CircleNum && i < JxtPage.m_Circle.size(); i++)
        {
            PwrDiagramObject.Circle CircleTemp=JxtPage.m_Circle.get(i);
            DrawCircle(CircleTemp, canvas);
        }
        for(int i =0; i < JxtPage.m_LineNum && i < JxtPage.m_Line.size(); i++)
        {
            PwrDiagramObject.Line LineTemp=JxtPage.m_Line.get(i);
            DrawLine(LineTemp, canvas);
        }
        for(int i = 0; i < JxtPage.m_ArcNum && i < JxtPage.m_Arc.size(); i++)
        {
            PwrDiagramObject.Arc ArcTemp=JxtPage.m_Arc.get(i);
            DrawArc(ArcTemp, canvas);
        }
        for(int i = 0; i < JxtPage.m_TextNum && i < JxtPage.m_Text.size(); i++)
        {
            PwrDiagramObject.Text TextTemp = JxtPage.m_Text.get(i);
            DrawText(TextTemp, canvas);
        }

        for(int i = 0; i < JxtPage.m_ElementNum && i < JxtPage.m_Element.size(); i++)
        {
            PwrDiagramObject.Element ElementTemp=JxtPage.m_Element.get(i);
            DrawElement(ElementTemp, canvas);
        }

        for(int i = 0; i < JxtPage.m_StateComptNum && i < JxtPage.m_StateCompt.size(); i++)
        {
            PwrDiagramObject.StateCompt StateComptTemp = JxtPage.m_StateCompt.get(i);
            DrawStateCompt(StateComptTemp, canvas, i);
        }

        for(int i = 0; i < JxtPage.m_ValueComptNum && i < JxtPage.m_ValueCompt.size(); i++)
        {
            PwrDiagramObject.ValueCompt ValueComptTemp=JxtPage.m_ValueCompt.get(i);
            DrawValueCompt(ValueComptTemp, canvas,i);
        }

        for(int i = 0; i < JxtPage.m_NormalBtnNum && i < JxtPage.m_NormalBtn.size(); i++) {
            PwrDiagramObject.NormalBtn BtnTemp=JxtPage.m_NormalBtn.get(i);
            DrawButton(BtnTemp, canvas);
        }
        */
        //Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources()，R.drawable.roadmech);

        SetShowType();

        return;
    }

    public void SetShowType() {
        setScaleX(m_FShowRate);
        setScaleY(m_FShowRate);
        setPivotX(m_CenterPnt.x);
        setPivotY(m_CenterPnt.y);
    }


    protected void DrawButton(PwrDiagramObject.NormalBtn BtnTemp, Canvas canvas) {
        if(BtnTemp==null) return;

        PointF StartPnt=GetPoint(BtnTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(BtnTemp.m_EndPoint);
        PointToView(canvas, EndPnt);

        int LineClr=GetColor(BtnTemp.m_TextColor);
        int FillClr=GetColor(BtnTemp.m_BackColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);//空心


        RectF rect = new RectF(StartPnt.x, StartPnt.y, EndPnt.x, EndPnt.y);
        canvas.drawRect(rect, paint);

        paint.setColor(FillClr);
        paint.setStyle(Paint.Style.FILL);//实心
        RectF FillRect = new RectF(StartPnt.x+5, StartPnt.y+5, EndPnt.x-5, EndPnt.y-5);
        canvas.drawRect(rect, paint);

        paint.setColor(LineClr);
        paint.setStrokeWidth(3);

        int Width=canvas.getWidth();
        int WidthEx=m_JxtCnfg.m_ScreenWidth;
        int FontSize = BtnTemp.m_FontSize * Width *2 / WidthEx;

        paint.setTextSize(FontSize);
        paint.setTextAlign(Paint.Align.CENTER);//中对齐
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();

        float baseline = (StartPnt.y+EndPnt.y - fontMetrics.bottom - fontMetrics.top) / 2;
        canvas.drawText(BtnTemp.m_BtnConText, (StartPnt.x+EndPnt.x)/2, baseline, paint);
    }

    protected void DrawValueCompt(PwrDiagramObject.ValueCompt ValueComptTemp, Canvas canvas, int Pos)
    {
        if(ValueComptTemp==null) return;

        PointF StartPnt=GetPoint(ValueComptTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(ValueComptTemp.m_EndPoint);
        PointToView(canvas,EndPnt);

        int TextClr=GetColor(ValueComptTemp.m_TextColor);
        int BackClr=GetColor(ValueComptTemp.m_BackColor);

        Paint paint = new Paint();
        paint.setColor(TextClr);
        paint.setStrokeWidth(3);

        int Width=canvas.getWidth();
        int WidthEx=m_JxtCnfg.m_ScreenWidth;
        int FontSize = ValueComptTemp.m_FontSize * Width *2 / WidthEx;

        paint.setTextSize(FontSize);

        String strFormat=String.format("%%.%df",ValueComptTemp.m_DecimalPlace);

        String strValue=String.format(strFormat,m_Value[m_JxtPos][Pos]);
        strValue+=" ";
        strValue+=ValueComptTemp.m_Unit;

        if(ValueComptTemp.m_AlignType==3) {
            paint.setTextAlign(Paint.Align.RIGHT);//右对齐
            canvas.drawText(strValue, EndPnt.x, StartPnt.y, paint);
        }
        else if(ValueComptTemp.m_AlignType==1) {
            paint.setTextAlign(Paint.Align.LEFT);//左对齐
            canvas.drawText(strValue, StartPnt.x, StartPnt.y, paint);
        }
        else if(ValueComptTemp.m_AlignType==2) {
            paint.setTextAlign(Paint.Align.CENTER);//中对齐
            Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();

            float baseline = (StartPnt.y+EndPnt.y - fontMetrics.bottom - fontMetrics.top) / 2;
            canvas.drawText(strValue, (StartPnt.x + EndPnt.x) / 2, baseline, paint);
        }
    }

    protected void DrawStateCompt(StateCompt StateComptTemp, Canvas canvas, int Pos)
    {
        if(StateComptTemp.m_StateNum<=0) return;
        if(StateComptTemp.m_DefaultState>=StateComptTemp.m_StateNum) return;
        PwrDiagramObject.Element ElementTemp=null;

        if(StateComptTemp.m_ShowType==1)
            ElementTemp=StateComptTemp.m_Element.get(m_State[m_JxtPos][Pos]);
        else if(StateComptTemp.m_ShowType==2) {
            if(m_State[m_JxtPos][Pos]==0) ElementTemp=StateComptTemp.m_Element.get(0);
            else if(m_State[m_JxtPos][Pos]==2) ElementTemp=StateComptTemp.m_Element.get(StateComptTemp.m_StateNum-1);
            else
            {
                ElementTemp=StateComptTemp.m_Element.get(StateComptTemp.m_ShowPos);
                StateComptTemp.m_ShowPos++;
                if(StateComptTemp.m_ShowPos>=StateComptTemp.m_StateNum-1)  StateComptTemp.m_ShowPos=1;
            }
        }

        DrawElement(ElementTemp,canvas);
    }

    protected void DrawElement(PwrDiagramObject.Element ElementTemp, Canvas canvas)
    {
        for(int i=0;i<ElementTemp.m_RectNum && i<ElementTemp.m_Rect.size();i++) {
            PwrDiagramObject.Rect RectTemp= ElementTemp.m_Rect.get(i);
            String StartPnt=RectTemp.m_StartPoint;
            String EndPnt=RectTemp.m_EndPoint;
            RectTemp.m_StartPoint=GetElementPoint(ElementTemp,RectTemp.m_StartPoint);
            RectTemp.m_EndPoint=GetElementPoint(ElementTemp,RectTemp.m_EndPoint);
            DrawRect(RectTemp, canvas);
            RectTemp.m_StartPoint = StartPnt;
            RectTemp.m_EndPoint = EndPnt;
        }
        for(int i=0;i<ElementTemp.m_CircleNum && i<ElementTemp.m_Circle.size();i++) {
            PwrDiagramObject.Circle CircleTemp=ElementTemp.m_Circle.get(i);
            String StartPnt=CircleTemp.m_StartPoint;
            String EndPnt=CircleTemp.m_EndPoint;
            CircleTemp.m_StartPoint=GetElementPoint(ElementTemp,CircleTemp.m_StartPoint);
            CircleTemp.m_EndPoint=GetElementPoint(ElementTemp,CircleTemp.m_EndPoint);
            DrawCircle(CircleTemp, canvas);
            CircleTemp.m_StartPoint = StartPnt;
            CircleTemp.m_EndPoint = EndPnt;
        }
        for(int i=0;i<ElementTemp.m_ArcNum && i<ElementTemp.m_Arc.size();i++) {
            PwrDiagramObject.Arc ArcTemp= ElementTemp.m_Arc.get(i);
            String StartPnt=ArcTemp.m_StartPoint;
            String EndPnt=ArcTemp.m_EndPoint;
            ArcTemp.m_StartPoint=GetElementPoint(ElementTemp,ArcTemp.m_StartPoint);
            ArcTemp.m_EndPoint=GetElementPoint(ElementTemp,ArcTemp.m_EndPoint);
            DrawArc(ArcTemp, canvas);
            ArcTemp.m_StartPoint = StartPnt;
            ArcTemp.m_EndPoint = EndPnt;
        }
        for(int i=0;i<ElementTemp.m_LineNum && i<ElementTemp.m_Line.size();i++) {
            PwrDiagramObject.Line LineTemp=ElementTemp.m_Line.get(i);
            String StartPnt=LineTemp.m_StartPoint;
            String EndPnt=LineTemp.m_EndPoint;
            LineTemp.m_StartPoint=GetElementPoint(ElementTemp,LineTemp.m_StartPoint);
            LineTemp.m_EndPoint=GetElementPoint(ElementTemp,LineTemp.m_EndPoint);
            DrawLine(LineTemp, canvas);
            LineTemp.m_StartPoint = StartPnt;
            LineTemp.m_EndPoint = EndPnt;
        }
        for(int i=0;i<ElementTemp.m_TextNum && i<ElementTemp.m_Text.size();i++) {
            PwrDiagramObject.Text TextTemp=ElementTemp.m_Text.get(i);
            String StartPnt=TextTemp.m_StartPoint;
            String EndPnt=TextTemp.m_EndPoint;
            TextTemp.m_StartPoint=GetElementPoint(ElementTemp,TextTemp.m_StartPoint);
            TextTemp.m_EndPoint=GetElementPoint(ElementTemp,TextTemp.m_EndPoint);
            DrawText(TextTemp, canvas);
            TextTemp.m_StartPoint = StartPnt;
            TextTemp.m_EndPoint = EndPnt;
        }
    }

    protected String GetElementPoint(PwrDiagramObject.Element ElementTemp, String strPoint)
    {
        PointF Pnt=new PointF(-1,-1);
        String[] strPos = strPoint.split(",");
        if(strPos.length!=2) return null;
        Pnt.x = Float.parseFloat(strPos[0]);
        Pnt.y = Float.parseFloat(strPos[1]);

        PointF PntElementS=new PointF(-1,-1);
        String[] strPosElementS = ElementTemp.m_StartPoint.split(",");
        if(strPosElementS.length!=2) return null;
        PntElementS.x = Float.parseFloat(strPosElementS[0]);
        PntElementS.y = Float.parseFloat(strPosElementS[1]);

        PointF PntElementE=new PointF(-1,-1);
        String[] strPosElementE =  ElementTemp.m_EndPoint.split(",");
        if(strPosElementE.length!=2) return null;
        PntElementE.x = Float.parseFloat(strPosElementE[0]);
        PntElementE.y = Float.parseFloat(strPosElementE[1]);

        float x = Pnt.x * (PntElementE.x-PntElementS.x)/1000 + PntElementS.x;
        float y = Pnt.y * (PntElementE.y-PntElementS.y)/1000 + PntElementS.y;

        String strResult="";
        strResult+=Float.toString(x);
        strResult+=",";
        strResult+=Float.toString(y);
        return strResult;
    }


    protected void DrawRect(PwrDiagramObject.Rect RectTemp, Canvas canvas)
    {
        if(RectTemp==null) return;

        PointF StartPnt=GetPoint(RectTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(RectTemp.m_EndPoint);
        PointToView(canvas, EndPnt);

        int LineClr=GetColor(RectTemp.m_LineColor);
        int FillClr=GetColor(RectTemp.m_FillColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(RectTemp.m_LineWidth);
        if(RectTemp.m_FillSign==0)
            paint.setStyle(Paint.Style.STROKE);//空心
        else if(LineClr==FillClr)
            paint.setStyle(Paint.Style.FILL);//实心
        else
            paint.setStyle(Paint.Style.STROKE);//空心

        RectF rect = new RectF(StartPnt.x, StartPnt.y, EndPnt.x, EndPnt.y);
        canvas.drawRect(rect, paint);

        if(RectTemp.m_FillSign==1 && FillClr!=LineClr) {
            paint.setColor(FillClr);
            paint.setStyle(Paint.Style.FILL);//实心
            RectF FillRect = new RectF(StartPnt.x+RectTemp.m_LineWidth, StartPnt.y+RectTemp.m_LineWidth, EndPnt.x-RectTemp.m_LineWidth, EndPnt.y-RectTemp.m_LineWidth);
            canvas.drawRect(rect, paint);
        }
    }

    protected void DrawCircle(PwrDiagramObject.Circle CircleTemp, Canvas canvas)
    {
        if(CircleTemp==null) return;

        PointF StartPnt=GetPoint(CircleTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(CircleTemp.m_EndPoint);
        PointToView(canvas, EndPnt);

        int LineClr=GetColor(CircleTemp.m_LineColor);
        int FillClr=GetColor(CircleTemp.m_FillColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(CircleTemp.m_LineWidth);
        if(CircleTemp.m_FillSign==0)
            paint.setStyle(Paint.Style.STROKE);//空心
        else if(LineClr==FillClr)
            paint.setStyle(Paint.Style.FILL);//实心
        else
            paint.setStyle(Paint.Style.STROKE);//空心

        canvas.drawCircle((StartPnt.x + EndPnt.x) / 2, (StartPnt.y + EndPnt.y) / 2, Math.abs(EndPnt.y - StartPnt.y) / 2, paint);

        if(CircleTemp.m_FillSign==1 && FillClr!=LineClr) {
            paint.setColor(FillClr);
            paint.setStyle(Paint.Style.FILL);//实心
            StartPnt.x+=CircleTemp.m_LineWidth;
            StartPnt.y+=CircleTemp.m_LineWidth;
            EndPnt.x-=CircleTemp.m_LineWidth;
            EndPnt.y-=CircleTemp.m_LineWidth;
            canvas.drawCircle((StartPnt.x + EndPnt.x) / 2, (StartPnt.y + EndPnt.y) / 2, Math.abs(EndPnt.y - StartPnt.y) / 2 + 1, paint);
        }
    }

    protected void DrawLine(PwrDiagramObject.Line LineTemp, Canvas canvas)
    {
        if(LineTemp==null) return;

        PointF StartPnt=GetPoint(LineTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(LineTemp.m_EndPoint);
        PointToView(canvas,EndPnt);

        int LineClr=GetColor(LineTemp.m_LineColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(LineTemp.m_LineWidth);

        canvas.drawLine(StartPnt.x, StartPnt.y, EndPnt.x, EndPnt.y, paint);
    }

    protected void DrawArc(PwrDiagramObject.Arc ArcTemp, Canvas canvas)
    {
        if(ArcTemp==null) return;

        PointF StartPnt=GetPoint(ArcTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(ArcTemp.m_EndPoint);
        PointToView(canvas,EndPnt);

        int LineClr=GetColor(ArcTemp.m_LineColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(ArcTemp.m_LineWidth);
        paint.setStyle(Paint.Style.STROKE);

        RectF rect = new RectF(StartPnt.x, StartPnt.y, EndPnt.x, EndPnt.y);

        canvas.drawArc(rect, //弧线所使用的矩形区域大小
                ArcTemp.m_StartAngle,  //开始角度
                ArcTemp.m_EndAngle-ArcTemp.m_StartAngle, //扫过的角度
                false, //是否使用中心
                paint);
    }


    protected void DrawText(PwrDiagramObject.Text TextTemp, Canvas canvas)
    {
        if(TextTemp==null) return;

        PointF StartPnt=GetPoint(TextTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(TextTemp.m_EndPoint);
        PointToView(canvas,EndPnt);

        int TextClr=GetColor(TextTemp.m_TextColor);
        int BackClr=GetColor(TextTemp.m_BackColor);

        Paint paint = new Paint();
        paint.setColor(TextClr);
        paint.setStrokeWidth(3);

        int Width=canvas.getWidth();
        int WidthEx=m_JxtCnfg.m_ScreenWidth;
        int FontSize = TextTemp.m_FontSize * Width *2 / WidthEx;
        paint.setTextSize(FontSize);

        if(TextTemp.m_AlignType==3) {
            paint.setTextAlign(Paint.Align.RIGHT);//右对齐
            canvas.drawText(TextTemp.m_Context, EndPnt.x, StartPnt.y, paint);
        }
        else if(TextTemp.m_AlignType==1) {
            paint.setTextAlign(Paint.Align.LEFT);//左对齐
            canvas.drawText(TextTemp.m_Context, StartPnt.x, StartPnt.y, paint);
        }
        else if(TextTemp.m_AlignType==2) {
            paint.setTextAlign(Paint.Align.CENTER);//中对齐
            Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();

            float baseline = (StartPnt.y+EndPnt.y - fontMetrics.bottom - fontMetrics.top) / 2;
            canvas.drawText(TextTemp.m_Context, (StartPnt.x + EndPnt.x) / 2, baseline, paint);
        }
    }

    protected void PointToView(Canvas canvas,PointF Pnt){

        int Width=canvas.getWidth();
        m_Width=canvas.getWidth();
        int WidthEx=m_JxtCnfg.m_ScreenWidth;

        Pnt.x=Pnt.x * Width  / WidthEx;
        Pnt.y=Pnt.y * Width  / WidthEx;
    }

    protected int GetColor(String strColor) {
        int Result = Color.argb(255,255,255,255);
        String[] strClr = strColor.split(",");
        if(strClr.length!=3) return Result;
        int Red = Integer.parseInt(strClr[0]);
        int Green = Integer.parseInt(strClr[1]);
        int Blue = Integer.parseInt(strClr[2]);
        Result = Color.argb(255,Red,Green,Blue);
        return Result;
    }

    protected PointF GetPoint(String strPoint) {
        PointF Pnt=new PointF(-1,-1);
        String[] strPos = strPoint.split(",");
        if(strPos.length!=2) return Pnt;
        Pnt.x = Float.parseFloat(strPos[0]);
        Pnt.y = Float.parseFloat(strPos[1]);
        return Pnt;
    }


}

